package com.xiao.service.Impl;

import com.xiao.dao.PublishMessageMapper;
import com.xiao.pojo.*;
import com.xiao.service.PublishMessageService;
import com.xiao.util.PublishUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PublishMessageServiceImpl implements PublishMessageService {
    @Autowired
    private PublishMessageMapper publishMessageMapper;
    //        根据随机数生成messageid
    private Long b = System.currentTimeMillis() + 1;
    //        根据随机数生成topicic
    private Long c = System.currentTimeMillis() + 2;
    //        根据随机数生成replyid
    private Long d = System.currentTimeMillis() + 2;
    //-----------------------------------------------------------------
    //插入通知
    @Override
    public boolean publishNotice(Message message, Long[] classid) {
        List<Message> list1 = new ArrayList<>();
        List<Consultinfo> list2 = new ArrayList<>();
        PublishUtil.insertMesCon(message, list1, list2, classid, publishMessageMapper);
        return (publishMessageMapper.insertMessageNot(list1) > 0 && publishMessageMapper.insertConsultinfo(list2) > 0);
    }
    //-----------------------------------------------------------------
    //    插入打卡
    @Override
    public boolean publishClock(Message message, Long[] classid) {
        List<Message> list1 = new ArrayList<>();
        List<Consultinfo> list2 = new ArrayList<>();
        PublishUtil.insertMesCon(message, list1, list2, classid, publishMessageMapper);
        return (publishMessageMapper.insertConsultinfo(list2) > 0 && publishMessageMapper.insertMessageClo(list1) > 0);
    }
    //-----------------------------------------------------------------
    //      插入讨论
    @Override
    public boolean publishDiscuss(Message message, Long[] classid) {
        List<Message> list1 = new ArrayList<>();
        List<Consultinfo> list2 = new ArrayList<>();
        PublishUtil.insertMesCon(message, list1, list2, classid, publishMessageMapper);
        return (publishMessageMapper.insertConsultinfo(list2) > 0 && publishMessageMapper.insertMessageDis(list1) > 0);
    }
    //    回复讨论
    @Override
    public boolean responseDiscuss(DiscussInfo discussInfo) {
        return publishMessageMapper.insertDis(new DiscussInfo(discussInfo.getMessageid(), discussInfo.getUid(), discussInfo.getSpeakcontent(), d)) > 0;
    }
    //    回复他人的回复
    @Override
    public boolean responseRespon(ReplyInfo replyInfo) {
        return publishMessageMapper.responseRespon(replyInfo)>0;
    }

    //-----------------------------------------------------------------
    //插入调查
    @Override
    public boolean publishExamine(Message message, Long[] classid, Topic[] topic) {
        List<Message> list = new ArrayList<>();
        List<Topic> list1 = new ArrayList<>();
        List<Consultinfo> list2 = new ArrayList<>();
        PublishUtil.insertMesCon(message, list, list2, classid, publishMessageMapper);
        for (Topic i : topic) {
            list1.add(new Topic(c, i.getTopicContent(), i.getOptionContent()));
        }
        return (((publishMessageMapper.insertExamine(new Examine(b, c))) > 0) && (publishMessageMapper.insertMessageExm(list) > 0) && (publishMessageMapper.insertConsultinfo(list2) > 0) && (publishMessageMapper.insertTopic(list1) > 0));
    }


}
